package com.coin900.coin.cmd.user;

import com.google.gson.Gson;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.fu.IFuUserService;
import com.value.utils.Md5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author shui
 * @create 2017-11-30
 **/
@Controller
public class FuUserPostCmd extends ACmd {

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        FuUserVO userVO = new Gson().fromJson(source, FuUserVO.class);

        String password = userVO.getPassword();
        String repassword = userVO.getRepassword();
        String username = userVO.getUsername();
        if(username == null){
            respSign.setMsg("用户名不能为空");
            respSign.setRespCode(RespCode.FAIL);
            return respSign;
        }
        if(password == null || ! password.equals(repassword)){
            respSign.setMsg("密码错误");
            respSign.setRespCode(RespCode.FAIL);
            return respSign;
        }

        userVO.setName(username);
        userVO.setCreatedInfor(user);
        userVO.setUserType(FuConstants.FU_USER_TYPE_TOKENOK);
        this.userService.insertFuUser(userVO);
        return respSign;
    }

    @Autowired
    private IFuUserService userService;

    @Override
    public String getCmdCode() {
        return CmdCode.FU_USER_POST;
    }
}
